/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package com.moon.admin.login.service;

import java.util.List;
import java.util.stream.Collectors;

import com.moon.admin.framework.secure.AuthUtils;
import com.moon.admin.framework.util.JsonUtils;
import com.moon.admin.framework.util.MoonException;
import com.moon.admin.framework.util.SecurityUtils;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.moon.admin.framework.secure.domain.AuthInfo;
import com.moon.admin.login.LoginMapper;


/**
 *  登录服务
 * @author moon
 */
@Service
public class LoginService{
	
	@Autowired
	private LoginMapper loginMapper;

	/**
	 * 根据账号密码登录
	 */
	public String login(String account, String password){
		SysUserEntity userEntity = loginMapper.getUserIdByAccount(account, SecurityUtils.md5(password));
		if(userEntity==null){
			throw new MoonException("账号或密码不正确");
		}
		List<String> roleCodes = loginMapper.getRoleCodesByUserId(userEntity.getId());
		AuthInfo info = ToolUtils.copy(userEntity,AuthInfo.class);
		info.setRoleCodes(roleCodes.stream().collect(Collectors.joining()));
		AuthUtils.setAuthInfo(info);
		String token = SecurityUtils.aesEncrypt(JsonUtils.obj2string(info));
		return token;
	}
}